﻿/*
 * Test classes for SSAS Partition Manager 
 * https://ssaspartitionmanager.codeplex.com/
 * 
 * Written by Dr. John Tunnicliffe of Decision Analytics, May 2015
 * eMail: john@decision-analytics.co.uk 
 * http://www.decision-analytics.co.uk/
 * https://www.linkedin.com/in/drjohntunnicliffe
 * 
 * Tests written with FluentAssertions, XUnit and Moq which can be installed from Nuget.  
 * 
 * FluentAssertions
 * http://www.fluentassertions.com/ 
 * 
 * XUnit
 * http://xunit.github.io/
 * 
 * Moq
 * https://github.com/Moq/moq4
 */

namespace AmoExtensions.IntegrationTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Dynamic;
    
    using AmoExtensions;
    using FluentAssertions;
    using Xunit;
    using Moq;
    
    public class PartitionManagerMultidimensionalFixture : IDisposable
    {
        public readonly PartitionManager partitionManager;
        public readonly string SsasDatabaseName;
        public const string SsasCubeName = "Adventure Works";
        public readonly IAmoServer SsasServer;

        /// <summary>
        /// As per the xUnit.net mantra, test setup and teardown is done in the constructor and Dispose() methods respectively
        /// </summary>
        public PartitionManagerMultidimensionalFixture()
        {
            SsasServer = new AmoServer();
            if (FixtureHelperFunctions.ConnectSsasServer(SsasServer))
            {
                // generate a name and deploy the cube for testing
                SsasDatabaseName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();

                if (FixtureHelperFunctions.DeployTestCube(SsasServer, SsasDatabaseName))
                {
                    // setup fixture data here

                    // ok let's start by getting the normal list of partitions to deploy and then edit the entries
                    // we are restricting the list to Internet Sales which is 16 quarterly partitions
                    string sql = @"
select
	SsasDatabaseName,
    SsasModelType,
	SsasCubeName,
	SsasMeasureGroupName,
	PartitionName,
    PartitionWhereClause,
    PartitionSlice
from spm.SsasPartitions 
where SsasMeasureGroupName = N'Internet Sales' and SsasModelType=N'Multidimensional'
";

                    IEnumerable<Partition> ListOfPartitions = FixtureHelperFunctions.QueryDynamic(sql)
                        .Select(x => Partition.FromDynamic(SsasServer, x))
                        .Cast<Partition>()  // cast the dynamic object into a partition object
                        .ToList(); // convert everything to a list object

                    // correct the database name
                    foreach (Partition partition in ListOfPartitions)
                    {
                        partition.DatabaseName = SsasDatabaseName;
                        // we now setup specific test data by changing specific entries

                        // the following four partitions will not be valid
                        if (partition.PartitionName == "Internet Sales 2011Q1")
                        {
                            partition.DatabaseName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();
                        }

                        if (partition.PartitionName == "Internet Sales 2011Q2")
                        {
                            partition.CubeName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();
                        }

                        if (partition.PartitionName == "Internet Sales 2011Q3")
                        {
                            partition.MeasureGroupName = FixtureHelperFunctions.GetUniqueSsasDatabaseName();
                        }

                        if (partition.PartitionName == "Internet Sales 2011Q4")
                        {
                            partition.PartitionWhereClause = null;
                        }
                        
                        if (partition.PartitionName == "Internet Sales 2012Q1")
                        {
                            // this does not have a template partition, so should be invalid
                            partition.MeasureGroupName = "Exchange Rates";
                        }
                    }

                    partitionManager = new PartitionManager(SsasServer, ListOfPartitions);
                }
            }
        }

        public void Dispose()
        {
            // drop the test OLAP database  
            SsasServer.DropDatabase(SsasDatabaseName);
            
            // disconnect from the server
            SsasServer.Dispose();
        }
    }
}
